package com.cskaoyan.app.controller;

import com.cskaoyan.app.bean.common.BasePageInfo;
import com.cskaoyan.app.bean.common.BaseRespVo;
import com.cskaoyan.app.bean.common.CommonData;
import com.cskaoyan.app.bean.po.MarketAd;
import com.cskaoyan.app.exception.ValidationException;
import com.cskaoyan.app.service.AdminAdService;
import com.cskaoyan.app.util.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("admin/ad")
public class AdminAdController {

    @Autowired
    private AdminAdService adminAdService;

    @GetMapping("list")
    public Object list(@Valid BasePageInfo basePageInfo, BindingResult bindingResult, String name, String content) throws ValidationException {
        ValidationUtil.valid(bindingResult);
        CommonData<MarketAd> data = adminAdService.list(basePageInfo, name, content);
        return BaseRespVo.ok(data);
    }

    @PostMapping("create")
    public Object create(@RequestBody MarketAd marketAd) {
        MarketAd data = adminAdService.create(marketAd);
        return BaseRespVo.ok(data);
    }

    @PostMapping("update")
    public Object update(@RequestBody MarketAd marketAd) {
        adminAdService.update(marketAd);
        return BaseRespVo.ok(marketAd);
    }

    @PostMapping("delete")
    public Object delete(@RequestBody MarketAd marketAd) {
        adminAdService.delete(marketAd);
        return BaseRespVo.ok();
    }
}
